/**
*  \file switch.c
*
*  \brief
*
*  \author Dajin Li
*
*
*/
/*===============================================[ private includes  ]================================================*/

#include <xmc4500/switch_hw.h>
#include "xmc_gpio.h"



/*===============================================[ private defines   ]================================================*/
#define p3_3_input()             XMC_GPIO_GetInput(P3_3)     /*PARK_BRAKE_IN_D_CPU_V*/
#define p2_0_input()             XMC_GPIO_GetInput(P2_0)     /*RESERVE_1_IN_D_CPU_V*/
#define p2_8_input()             XMC_GPIO_GetInput(P2_8)     /*MAIN_CONTACTOR_ERROR*/ 
#define p2_9_input()             XMC_GPIO_GetInput(P2_9)     /*CUT-OFF_ISO_VALVE_ERROR*/
#define p2_10_input()            XMC_GPIO_GetInput(P2_10)    /*BUZZER_ERROR*/ 
#define p5_2_input()             XMC_GPIO_GetInput(P5_2)     /*FAN_ERROR*/
#define p0_1_input()             XMC_GPIO_GetInput(P0_1)     /*FORWARD_IN_D_CPU_V*/ 
#define p1_0_input()             XMC_GPIO_GetInput(P1_0)     /*/BOOT_ANCHOR_CPU_M*/ 
#define p1_1_input()             XMC_GPIO_GetInput(P1_1)     /*SEAT_SWITCH_NC_IN_D_CPU_V*/
#define p1_8_input()             XMC_GPIO_GetInput(P1_8)     /*NEUTRAL_POSITION_IN_D_CPU_V*/
#define p1_10_input()            XMC_GPIO_GetInput(P1_10)    /*REVERSE_IN_D_CPU_V*/ 
#define p1_11_input()            XMC_GPIO_GetInput(P1_11)    /*LIFT_IN_D_CPU_V*/
#define p1_12_input()            XMC_GPIO_GetInput(P1_12)    /*TILT_IN_D_CPU_V*/ 
#define p1_13_input()            XMC_GPIO_GetInput(P1_13)    /*MAST_AUX_1_IN_D_CPU_V*/
#define p1_14_input()            XMC_GPIO_GetInput(P1_14)    /*MAST_AUX_2_IN_D_CPU_V*/ 
#define p1_15_input()            XMC_GPIO_GetInput(P1_15)    /*SEAT_SWITCH_IN_D_CPU_V*/ 
#define p3_1_input()             XMC_GPIO_GetInput(P3_1)     /*CPU_SYNC*/ 
#define p3_2_input()             XMC_GPIO_GetInput(P3_2)     /*TLF_INT*/ 
#define p3_4_input()             XMC_GPIO_GetInput(P3_4)     /*TLF_SATESTATE1*/ 


/*===============================================[ private datatypes ]================================================*/





/*===============================================[ private variables ]================================================*/


/*===============================================[ public variables  ]================================================*/

/*================================================[ inline functions ]================================================*/





/*===============================================[ private functions ]================================================*/

/*================================================[ public functions ]================================================*/


/**
* @brief    This samples the switch inputs.
* @param    none
* @note     none
* @retval   u16: swtich value
*/
u32 get_switches(void)
{
    u32 switch_inputs = 0;

    switch_inputs |= ((u32)p3_3_input())  << 0;   /*PARK_BRAKE_IN_D_CPU_V*/
    switch_inputs |= ((u32)p2_0_input())  << 1;   /*RESERVE_1_IN_D_CPU_V*/
    switch_inputs |= ((u32)p2_8_input())  << 2;   /*MAIN_CONTACTOR_ERROR*/
    switch_inputs |= ((u32)p2_9_input())  << 3;   /*CUT-OFF_ISO_VALVE_ERROR*/
    switch_inputs |= ((u32)p2_10_input()) << 4;   /*BUZZER_ERROR*/
    switch_inputs |= ((u32)p5_2_input())  << 5;   /*FAN_ERROR*/
    switch_inputs |= ((u32)p0_1_input())  << 6;   /*FORWARD_IN_D_CPU_V*/ 
    switch_inputs |= ((u32)p1_0_input())  << 7;   /*/BOOT_ANCHOR_CPU_M*/ 
    switch_inputs |= ((u32)p1_1_input())  << 8;   /*SEAT_SWITCH_NC_IN_D_CPU_V*/
    switch_inputs |= ((u32)p1_8_input())  << 9;   /*NEUTRAL_POSITION_IN_D_CPU_V*/
    switch_inputs |= ((u32)p1_10_input()) << 10;  /*REVERSE_IN_D_CPU_V*/ 
    switch_inputs |= ((u32)p1_11_input()) << 11;  /*LIFT_IN_D_CPU_V*/
    switch_inputs |= ((u32)p1_12_input()) << 12;  /*TILT_IN_D_CPU_V*/ 
    switch_inputs |= ((u32)p1_13_input()) << 13;  /*MAST_AUX_1_IN_D_CPU_V*/
    switch_inputs |= ((u32)p1_14_input()) << 14;  /*MAST_AUX_2_IN_D_CPU_V*/ 
    switch_inputs |= ((u32)p1_15_input()) << 15;  /*SEAT_SWITCH_IN_D_CPU_V*/ 
    switch_inputs |= ((u32)p3_1_input())  << 16;  /*CPU_SYNC*/ 
    switch_inputs |= ((u32)p3_2_input())  << 17;  /*TLF_INT*/ 
    switch_inputs |= ((u32)p3_4_input())  << 18;  /*TLF_SATESTATE1*/ 
    
    return switch_inputs;
}
/**
* @brief    Enable led.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/

void led_enable(u8 enable)
{
    if(enable == 1)
    {
        XMC_GPIO_SetOutputLow(P3_6);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P3_6);
    }
}
/**
* @brief    Enable inv2.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/ 
void inv2_enable(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P2_14);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P2_14);
    }
} 
/**
* @brief    reset error enable
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/
void reset_error_enable(u8 enable)
{
    if(enable == 1)
    {
        XMC_GPIO_SetOutputLow(P5_7);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P5_7);
    }
} 
/**
* @brief    DCDC enable.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/

void dcdc_enable(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P0_4);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P0_4);
    }
} 
/**
* @brief    dclink precharge enable.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/
void dclink_precharge_enable(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P1_6);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P1_6);
    }
} 
/**
* @brief    Reserve2 in on enable.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/
void reserve2_in_on_enable(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P1_7);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P1_7);
    }
} 
/**
* @brief    Inv1 enable.
* @param    enable: 0 disable,  1 enable
* @note     none
* @retval   none
*/ 
void inv1_enable(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P1_9);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P1_9);
    }
}  


void output_tlf_error(u8 enable)
{
    if(enable == 0)
    {
        XMC_GPIO_SetOutputLow(P3_5);
    }
    else
    {
        XMC_GPIO_SetOutputHigh(P3_5);
    }

}

/*===============================================[ private functions ]================================================*/








/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

